package main

import (
	"context"
	"flag"
	"gitee.com/wochong/HalloGin/cmd/server/web"
	"github.com/golang/glog"
	"net/http"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"syscall"
)

var (
	confFile    = flag.String("conf", "../../conf/conf.yml", "The configure file")
	showVersion = flag.Bool("version", false, "show build version.")
	pprof       = flag.String("pprof", "", "[localhost:6060]start debug page.")
	buildStamp  = "UNKOWN"
	gitHash     = "UNKOWN"
	version     = "UNKOWN"
)

func main() {

	flag.Parse()       // 解析命令行参数
	defer glog.Flush() // 执行批量写入日志

	if *showVersion {
		glog.Infoln("Delivery version :", version)
		glog.Infoln("Git Commit Hash :", gitHash)
		glog.Infoln("UTC Build Time :", buildStamp)
		os.Exit(0)
	}

	glog.Infoln("当前Server版本: ", version)
	glog.Infoln(`Git Commit Hash :`, gitHash)
	glog.Infoln(`UTC Build Time :`, buildStamp)

	if *pprof != "" {
		go func() {
			err := http.ListenAndServe(*pprof, nil)
			glog.Infoln(err)
		}()
	}

	web.Init(*confFile, version) // 初始化服务

	glog.Infoln("Setup Server SUCCESS...")

	ch := make(chan os.Signal)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)

	ctx, cancel := context.WithCancel(context.Background())
	web.StartContext(ctx)

	<-ch
	//TODO 清理资源
	glog.Infoln("收到 ctrl+c 命令....")
	cancel()
	web.GracefulStop()

}
